package com.dmap.knowledge.controller.background;


import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.knowledge.controller.api.param.ArticleColumnEditParam;
import com.dmap.knowledge.controller.api.param.ArticleColumnParam;
import com.dmap.knowledge.po.ArticleColumn;
import com.dmap.knowledge.po.ArticleColumnApp;
import com.dmap.knowledge.service.ArticleColumnAppExtensionService;
import com.dmap.knowledge.service.ArticleColumnExtensionService;
import com.dmap.knowledge.service.ArticleColumnService;
import com.dmap.knowledge.vo.ArticleColumnCheckVo;
import com.dmap.knowledge.vo.ArticleColumnDownListVo;
import com.dmap.knowledge.vo.ArticleInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api(value = "文章专栏", description = "background/文章专栏")
@RestController
@RequestMapping(value = "/background/dmap-knowledge-server/v1/column")
public class ArticleColumnController {
    @Resource
    private ArticleColumnService articleColumnService;

    @Resource
    private ArticleColumnAppExtensionService articleColumnAppExtensionService;
    @Resource
    private ArticleColumnExtensionService articleColumnExtensionService;

    @ResponseBody
    @ApiOperation(value = "新增文章专栏", notes = "新增文章专栏")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public BaseVo insertColumn(@RequestBody(required = false) ArticleColumnParam articleColumnParam) {
        BaseVo result = new BaseVo();
        if (articleColumnParam.getLang() == null || articleColumnParam.getIconUrl() == null || articleColumnParam.getName() == null || articleColumnParam.getKey() == null || articleColumnParam.getStatus() == 0 || articleColumnParam.getComment() == null || articleColumnParam.getSceneId() == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(articleColumnParam.getLang()) || StringUtils.isBlank(articleColumnParam.getIconUrl()) || StringUtils.isBlank(articleColumnParam.getName()) || StringUtils.isBlank(articleColumnParam.getKey()) || !StringUtils.isNumeric(articleColumnParam.getSceneId())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleColumnService.insertArticleColumn(articleColumnParam);

        if (success == 0) {
            result.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
        }
        return result;

    }

    @ResponseBody
    @ApiOperation(value = "修改文章专栏", notes = "修改文章专栏")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseVo updateColumn(@RequestBody(required = false) ArticleColumnEditParam articleColumnEditParam) {
        BaseVo result = new BaseVo();
        if (articleColumnEditParam.getColumnId() == null || articleColumnEditParam.getLang() == null || articleColumnEditParam.getIconUrl() == null || articleColumnEditParam.getName() == null || articleColumnEditParam.getKey() == null || articleColumnEditParam.getStatus() == 0 || articleColumnEditParam.getComment() == null || articleColumnEditParam.getSceneId() == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (!StringUtils.isNumeric(articleColumnEditParam.getColumnId()) || StringUtils.isBlank(articleColumnEditParam.getLang()) || StringUtils.isBlank(articleColumnEditParam.getIconUrl()) || StringUtils.isBlank(articleColumnEditParam.getName()) || StringUtils.isBlank(articleColumnEditParam.getKey()) || StringUtils.isBlank(articleColumnEditParam.getComment()) || !StringUtils.isNumeric(articleColumnEditParam.getSceneId())) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleColumnService.updateArticleColumn(articleColumnEditParam);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        }
        
        return result;
    }

    @Transactional
    @ResponseBody
    @ApiOperation(value = "删除文章专栏", notes = "删除文章专栏")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public BaseVo deleteColumn(@RequestParam String columnIds) {
        BaseVo result = new BaseVo();
        if (columnIds == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(columnIds)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int success = articleColumnService.deleteColumnList(columnIds);
        if (success == 0) {
            result.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);

        }
        return result;

    }

    @ResponseBody
    @ApiOperation(value = "选择文章专栏", notes = "选择文章专栏")
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    public BaseVo selectColumn(@RequestParam String lang,
                               @RequestParam(required = false) String sceneId,
                               @RequestParam(required = false) String key,
                               @RequestParam(required = false, defaultValue = "0") long order,
                               @RequestParam int pageIndex,
                               @RequestParam int pageSize) {
        BaseVo result = new BaseVo();
        if (lang == null || pageIndex == 0 || pageSize == 0) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (key != null && StringUtils.isBlank(key)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (sceneId != null && !StringUtils.isNumeric(sceneId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(lang)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        ArticleInfoVo articleInfoVo = articleColumnExtensionService.selectArticleColumnList(lang, sceneId, key, order, pageIndex, pageSize);
        result.setData(articleInfoVo);
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "唯一性验证", notes = "唯一性验证")
    @RequestMapping(value = "/check", method = RequestMethod.GET)
    public BaseVo selectColumnCheck(@RequestParam String lang,
                                    @RequestParam(required = false) String name,
                                    @RequestParam(required = false) String key,@RequestParam(required = false) String columnId) {
        BaseVo result = new BaseVo();
        if (StringUtils.isBlank(lang)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (name != null && StringUtils.isBlank(name)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (key != null && StringUtils.isBlank(key)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (columnId!=null&&!StringUtils.isNumeric(columnId)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        int count = articleColumnExtensionService.checkColumnList(lang, name, key,columnId);
        ArticleColumnCheckVo articleColumnCheckVo = new ArticleColumnCheckVo();
        articleColumnCheckVo.setCheck(2);
        if (count != 0) {
            articleColumnCheckVo.setCheck(1);
            result.setData(articleColumnCheckVo);
        } else {
            result.setData(articleColumnCheckVo);
        }
        return result;
    }

    @ResponseBody
    @ApiOperation(value = "选择文章栏目下拉列表", notes = "选择文章栏目下拉列表")
    @RequestMapping(value = "/dropDown/list", method = RequestMethod.GET)
    public BaseVo selectArticleColumn(@RequestParam String lang,
                                      @RequestParam(required = false) String appNumber) {
        BaseVo result = new BaseVo();
        List<ArticleColumnDownListVo> articleColumnDownListVoList = new ArrayList<>();
        if (lang == null) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (appNumber != null && StringUtils.isBlank(appNumber)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (StringUtils.isBlank(lang)) {
            result.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            result.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return result;
        }
        if (appNumber != null) {
            List<ArticleColumnApp> articleColumnAppList = articleColumnAppExtensionService.selectArticleColumnAppDownList(lang, appNumber);
            articleColumnAppList.forEach(articleColumnApp -> {
                ArticleColumn articleColumn = articleColumnService.selectArticleColumnById(articleColumnApp.getArticleColumnId());
                if (articleColumn != null) {
                    ArticleColumnDownListVo articleColumnDownListVo = new ArticleColumnDownListVo(articleColumn);
                    articleColumnDownListVoList.add(articleColumnDownListVo);
                }
            });
        } else {
            List<ArticleColumn> articleColumnList = articleColumnExtensionService.selectArticleColumnDownList(lang);
            articleColumnList.forEach(articleColumn -> {
                ArticleColumnDownListVo articleColumnDownListVo = new ArticleColumnDownListVo(articleColumn);
                articleColumnDownListVoList.add(articleColumnDownListVo);
            });
        }
        result.setData(articleColumnDownListVoList);
        return result;
    }


}


